// import req from '@/utils/req';
import { uniqueId } from "@/server-lib/tools"
import { store } from "@/utils/index"
import req from "@/utils/req"
import { Message } from "@arco-design/web-vue"
import { data, boardData, review, barrage, sheetData, mindData } from "./mock"

// 整个文档的默认ID
const DATA_NAME = "FD_001"
// 文档内表单的命名前缀
const FORM_NAMESPACE = "FDF"
// 文档内白板的命名前缀
const BOARD_NAMESPACE = "FDB"
// 文档内电子表格的命名前缀
const SHEET_NAMESPACE = "FDS"
// 文档内思维导图的命名前缀
const MIND_NAMESPACE = "FDM"

// 获取页面数据
export const getDoc = (id: string = DATA_NAME) => {
	return store.get(id) || data
}

// 保存页面数据
export const addDoc = (data: any) => {
	return store.set(DATA_NAME, data)
}
// 更新文档内容
export const updateDoc = (data: any) => {
	store.set(DATA_NAME, data)
}

// 清空页面数据
export const clearDoc = (id = DATA_NAME) => {
	store.remove(id)
}

// 保存表单
export const saveForm = (pid = DATA_NAME, fid: string) => {
	console.log(pid, fid)
}

// 表单数据提交
export const addForm = (data: { form: any }) => {
	console.log("表单提交api", data)
}

// 删除表单

// 获取白板数据
export const getBoard = (id: string) => {
	return store.get(`${BOARD_NAMESPACE}_${id}`) || boardData
}

// 保存白板
export const addBoard = (id: string, data: any) => {
	const nid = id || uniqueId()
	store.set(`${BOARD_NAMESPACE}_${nid}`, data)
	return nid
}
// 获取电子表格数据
export const getSheet = (id: string) => {
	return store.get(`${SHEET_NAMESPACE}_${id}`) || sheetData
}

// 保存电子表格
export const addSheet = (id: string, data: any) => {
	const nid = id || uniqueId()
	store.set(`${SHEET_NAMESPACE}_${nid}`, data)
	return nid
}
// 获取导图数据
export const getMind = (id: string) => {
	return store.get(`${MIND_NAMESPACE}_${id}`) || mindData
}

// 保存思维导图
export const addMind = (id: string, data: any) => {
	const nid = id || uniqueId()
	store.set(`${MIND_NAMESPACE}_${nid}`, data)
	return nid
}

// 添加评论
export const addReview = (data: any) => {
	const list = store.get(`${DATA_NAME}_review`) || []
	list.push(data)
	store.set(`${DATA_NAME}_review`, list)
}

// 获取评论列表
export const getReview = (id = DATA_NAME) => {
	return store.get(`${id}_review`) || review
}

// 添加弹幕
export const addBarrage = (data: any) => {
	const list = store.get(`${DATA_NAME}_barrage`) || []
	list.push(data)
	store.set(`${DATA_NAME}_barrage`, list)
}

// 获取评论列表
export const getBarrage = (id = DATA_NAME) => {
	return store.get(`${id}_barrage`) || barrage
}

// 划词评论接口
// 1. 获取划词评论数据
// id 文档ID
// mark_id 划词ID
export const getWordReview = (id = DATA_NAME, mark_id?: string) => {
	const list = store.get(`${id}_word_review`) || []
	return mark_id ? list.filter((item: any) => item.mid === mark_id).reverse() : list
}
// 2. 保存划词评论数据
export const addWordReview = (id = DATA_NAME, data: any) => {
	const list = store.get(`${id}_word_review`) || []
	const item_id = uniqueId()
	const time = Date.now()
	list.push({ ...data, id: item_id, ct: time })
	store.set(`${DATA_NAME}_word_review`, list)
}

// 版本管理
function generateNextVersion(currentVersion = "0.0.0") {
	// 将当前版本拆分为数组
	let [major, minor, patch] = currentVersion.split(".").map(Number)

	// 增加补丁版本
	patch++

	// 检查是否需要进位
	if (patch > 9) {
		patch = 0
		minor++

		if (minor > 9) {
			minor = 0
			major++
		}
	}

	// 格式化并返回新版本，不补零
	return `${major}.${minor}.${patch}`
}
// 保存版本
export const saveVersion = (data: any) => {
	const list = store.get(`${DATA_NAME}_v`) || []
	// 如果版本数超过15, 则删除最旧版本
	if (list.length > 15) {
		list.shift()
	}

	list.unshift({
		ct: Date.now(),
		v: generateNextVersion(list[0]?.v || "0.0.0"),
		...data,
	})
	store.set(`${DATA_NAME}_v`, list)
}
// 获取版本列表
export const getVersion = (id = DATA_NAME) => {
	return store.get(`${id}_v`) || []
}

